package com.mengmablog.blog.controller;

import com.mengmablog.blog.commons.BlogConst;
import com.mengmablog.blog.commons.ServerResponse;
import com.mengmablog.blog.service.port.IArticlesService;
import com.mengmablog.blog.service.port.ICommentsService;
import com.mengmablog.blog.service.port.IMateaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("api")
@RestController
public class ClientController {
    @Autowired
    private IArticlesService iArticlesService;

    @Autowired
    private ICommentsService iCommentsService;

    @Autowired
    private IMateaService iMateaService;

    /**
     * 文章列表
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("article")
    public ServerResponse article(@RequestParam(required = false, defaultValue = BlogConst.PAGE) Integer page,
                                  @RequestParam(required = false, defaultValue = BlogConst.LIMIT) Integer limit) {
        return iArticlesService.article(page, limit);
    }

    /**
     * 文章详细页面内容
     *
     * @param id
     * @return
     */
    @GetMapping("article/{id}")
    public ServerResponse article_id(@PathVariable Integer id) {
        return iArticlesService.article_id(id,"client");
    }

    /**
     * 获取文章评论
     * @param articleId
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("comment")
    public ServerResponse comment(@RequestParam(required = true) String articleId,
                                  @RequestParam(required = false, defaultValue = BlogConst.PAGE) Integer page,
                                  @RequestParam(required = false, defaultValue = BlogConst.LIMIT) Integer limit) {
        return iCommentsService.comment(articleId, page, limit);
    }

    /**
     * 发表评论
     * @param articleId
     * @param pid
     * @param content
     * @param name
     * @param website
     * @param email
     * @return
     */
    @PostMapping("comment")
    public ServerResponse comment(@RequestParam(required = true) Integer articleId,
                                  @RequestParam(required = false) Integer pid,
                                  @RequestParam(required = true) String content,
                                  @RequestParam(required = true) String name,
                                  @RequestParam(required = false) String website,
                                  @RequestParam(required = false) String email) {
        return iCommentsService.comment(articleId, pid, content, name, website, email);

    }

    /**
     *顶或踩评论
     * @param commentId
     * @param assess
     * @return
     */
    @PostMapping("comment/{commentId}/assess")
    public ServerResponse comment_commentId_assess(@PathVariable String commentId,
                                                   @RequestParam(required = true) String assess){
        return iCommentsService.comment_commentId_assess(commentId,assess);
    }

    /**
     * 分类
     * @return
     */
    @GetMapping("category")
    public ServerResponse category(){
        return iMateaService.category_tag("category");
    }

    /**
     * 标签
     * @return
     */
    @GetMapping("tag")
    public ServerResponse tag(){
        return iMateaService.category_tag("tag");

    }

    /**
     * 归档
     * @return
     */
    @GetMapping("archive")
    public ServerResponse archive(){
        return iArticlesService.archive();
    }
}
